package com.sunday.haowushop.ui;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sunday.common.base.BaseActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.widgets.RecyclerTabLayout;
import com.sunday.haowushop.entity.LimitTime;
import com.sunday.haowushop.http.ApiClient;


import java.util.ArrayList;
import java.util.List;
import com.sunday.haowushop.R;
import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 刘涛 on 2017/5/2.
 */

public class TestActivity extends BaseActivity {


    @Bind(R.id.recycler_tablayout)
    RecyclerTabLayout recyclerTablayout;
    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;

    Tabadapter tabadapter;
    private List<LimitTime> timeList=new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        ButterKnife.bind(this);
        tabadapter=new Tabadapter(timeList);
        recyclerTablayout.setAutoSelectionMode(true);
        recyclerTablayout.setAdapter(tabadapter);
        getLimitTimes();

    }

    private void getLimitTimes(){
        Call<ResultDO<List<LimitTime>>> call = ApiClient.getApiAdapter().getLimitTimeList();
        call.enqueue(new Callback<ResultDO<List<LimitTime>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<LimitTime>>> call, Response<ResultDO<List<LimitTime>>> response) {
                if (isFinish) {
                    return;
                }
                ResultDO<List<LimitTime>> resultDO = response.body();
                if (resultDO == null) {
                    return;
                }
                if (resultDO.getCode() == 0) {
                    timeList.addAll(resultDO.getResult());
                    tabadapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<LimitTime>>> call, Throwable t) {

            }
        });
    }

    class Tabadapter extends RecyclerView.Adapter {


        private List<LimitTime> dataSet = new ArrayList<>();

        public Tabadapter(List<LimitTime> datas) {
            dataSet = datas;
        }

        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.time_tab_item, null);
            return new TabHolder(view);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
            TabHolder tabHolder= (TabHolder) holder;
            LimitTime item=dataSet.get(position);
            tabHolder.timeTitle.setText(item.getStartTime()+"");
            tabHolder.timeExtra.setText(item.getTime());
        }

        @Override
        public int getItemCount() {
            return dataSet.size();
        }

        class TabHolder extends RecyclerView.ViewHolder {

            @Bind(R.id.time_title)
            TextView timeTitle;
            @Bind(R.id.time_extra)
            TextView timeExtra;

            public TabHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int pos = getAdapterPosition();

                    }
                });
            }
        }
    }


}
